const axios = require('axios');
const cheerio = require('cheerio');
const path = require('path');

// 定义要爬取的URL
const url = 'https://www.16888.com/';
let res = []
// carList从本地json文件读取，路径在当前目录下./car.json
const carListPath = path.join(__dirname, 'car.json');
const carList = require(carListPath).data;
const carListLength = carList.length;
console.log('carListLength:', carListLength);
// 记录开始时间
const startTime = Date.now();
class Config {
    constructor(brandName, name, carName, price, lowestPrice) {
        this.brandName = brandName;
        this.name = name;
        this.carName = carName;
        this.price = price;
        this.lowestPrice = lowestPrice;
    }
}
// sleep函数，用于模拟延迟
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
// 主异步函数
(async function fetchCarData(carList) {
    try {
        const results = [];

        for (const car of carList) {
            try {
                // 发起请求
                const response = await axios.get(`${url}${car.id}/`);
                if (response.status === 200) {
                    const html = response.data;
                    const $ = cheerio.load(html);
                    const $title = $('tr.last');
                    $title.each((index, brand) => {
                        const $childBrands = $(brand).children();
                        const name = $childBrands.eq(0).children('div').find('a').text();
                        console.log(name);
                        const price = $childBrands.eq(2).children('span').find('a').text().replace(/\n|\s/g, ''); // 去掉\n和空格
                        console.log(price);
                        const lowestPrice = $childBrands.eq(3).children('span').find('a').text().replace(/\n|\s/g, '');
                        console.log(lowestPrice);
                        const config = new Config(car.brandName, name, car.name, price, lowestPrice);
                        results.push(config);
                    });
                } else {
                    console.warn(`Request failed with status code ${response.status} for ID: ${car.id}`);
                }
            } catch (error) {
                console.error(`Error fetching URL for ID: ${car.id}, ${error.message}`);
            }

            // 模拟延迟
            await sleep(500); // 假设每次请求之间延迟500毫秒
        }

        // 输出结果
        console.log(results);

        // 如果需要写入文件的操作可以在这里进行
        const json = { data: results };
        const fs = require('fs');
        fs.writeFileSync(path.join(__dirname, 'carConfig.json'), JSON.stringify(json, null, 2));
        // 记录耗时，时分秒格式
        const endTime = Date.now();
        const duration = new Date(endTime - startTime);
        console.log(`Total execution time: ${duration} ms`);

    } catch (error) {
        console.error(`Overall error during fetch operations: ${error.message}`);
    }
})(carList); // 传入carList并立即执行这个异步函数


